package com.ywang.puremvc.impl;

import java.util.Hashtable;

import com.ywang.puremvc.exception.NoSupportedMediator;
import com.ywang.puremvc.framework.IMediator;
import com.ywang.puremvc.framework.IObserver;
import com.ywang.puremvc.framework.IView;

/**
 * @author ywang
 */
public class View implements IView {
    private static View instance;
    
    private Hashtable<String, IMediator> mediators;
    private Hashtable<String, IObserver> observers;
    
    public static View getInstance() {
        if (instance == null)
            instance = new View();
        return instance;
    }
    
    private View() {
        mediators = new Hashtable<String, IMediator>();
        observers = new Hashtable<String, IObserver>();
    }
    
    public IMediator retrieveMediator(String className) throws NoSupportedMediator {
        IMediator mediator = mediators.get(className);
        if (mediator == null) {
            try {
                Class<IMediator> clazz = (Class<IMediator>) Class.forName(className);
                mediator = clazz.newInstance();
                mediators.put(className, mediator);
            }
            catch (Exception e) {
                throw new NoSupportedMediator();
            }
        }
        return mediator;
    }
    
    public void removeMediator(String className) {
        mediators.remove(className);
    }
    
    public void registerObserver(IObserver observer) {
        
    }
}
